# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

from scrapy.conf import settings
import os
import urllib
import pymongo
from Umei.spiders import umei
from Umei.spiders import yixiuba
from Umei.spiders import redis

FILE_NAME = 'Umei'


class UmeiPipeline(object):
    def __init__(self):
        self.umeiDB = UmeiMongoDBManager()
        self.yixiubaDB = YixiubaMongoDBManager()
        self.redisYixiubaDB = RedisYixiubaMongoDBManager()

    def process_item(self, item, spider):

        # abs_dir_path = get_abs_path(item)
        # save_to_folder(item, abs_dir_path)
        if spider.name == umei.Umei.name:
            self.umeiDB.process_item(item, spider)
        elif spider.name == yixiuba.Yixiuba.name:
            self.yixiubaDB.process_item(item, spider)
        elif spider.name == redis.Yixiuba.name:
            self.redisYixiubaDB.process_item(item, spider)
        return item


def get_abs_path(item):
    """
    创建文件夹
    :param item: AlbumItem
    :return:
    """
    abs_path = os.path.join(os.getcwd(), FILE_NAME)
    # 确保文件夹已存在
    if not os.path.exists(abs_path) or not os.path.isdir(abs_path):
        os.mkdir(abs_path)

    # 确保 ‘相应文件夹’ 已被创建
    abs_path_ = os.path.join(abs_path, item['title'])
    if not os.path.exists(abs_path_) or not os.path.isdir(abs_path):
        os.mkdir(abs_path_)

    return abs_path_


def save_to_folder(item, abs_dir_path):
    """
    下载并保存图片至相应文件夹
    :param item: AlbumItem
    :param abs_dir_path: 文件夹绝对路径
    :return:
    """
    for url in item['images']:
        img_name = url.split('/')[-1]
        img_abs_path = os.path.join(abs_dir_path, img_name)
        if not os.path.exists(img_abs_path):
            urllib.urlretrieve(url, img_abs_path)


class UmeiMongoDBManager(object):

    def __init__(self):
        connection = pymongo.MongoClient(
            settings['MONGODB_SERVER'],
            settings['MONGODB_PORT']
        )
        db = connection[settings['MONGODB_DB']]
        # db.authenticate(settings['MONGODB_NAME'], settings['MONGODB_PASSWORD'])
        self.collection = db[settings['MONGODB_DOCNAME_UMEI']]

    def process_item(self, item, spider):
        self.collection.insert(dict(item))
        # log.msg("Question added to MongoDB database!", level=log.DEBUG, spider=spider)
        return item


class YixiubaMongoDBManager(object):

    def __init__(self):
        connection = pymongo.MongoClient(
            settings['MONGODB_SERVER'],
            settings['MONGODB_PORT']
        )
        db = connection[settings['MONGODB_DB']]
        self.collection = db[settings['MONGODB_DOCNAME_YIXIUBA']]

    def process_item(self, item, spider):
        self.collection.insert(dict(item))
        # log.msg("Question added to MongoDB database!", level=log.DEBUG, spider=spider)
        return item

class RedisYixiubaMongoDBManager(object):

    def __init__(self):
        connection = pymongo.MongoClient(
            settings['MONGODB_SERVER'],
            settings['MONGODB_PORT']
        )
        db = connection[settings['MONGODB_DB']]
        self.collection = db[settings['MONGODB_DOCNAME_REDISYIXIUBA']]

    def process_item(self, item, spider):
        self.collection.insert(dict(item))
        # log.msg("Question added to MongoDB database!", level=log.DEBUG, spider=spider)
        return item
